package net.guerlab.mall.seller.print.printer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.math.BigDecimal;
import java.math.RoundingMode;

import net.guerlab.mall.seller.print.entity.offline.GoodsInfo;
import net.guerlab.mall.seller.print.entity.offline.OrderInfo;
import static net.guerlab.mall.seller.print.printer.PrintHelper.getPoint;

public class OfflinePrintableImpl implements Printable {

    private static final String FONT_NAME = "新宋体";

    private static final int FONT_TYPE = Font.PLAIN;

    private OrderInfo orderInfo;

    private boolean isFirst = true;

    public OfflinePrintableImpl(OrderInfo orderInfo) {
        this.orderInfo = orderInfo;
    }

    /**
     * @param Graphic
     *            指明打印的图形环境
     * @param PageFormat
     *            指明打印页格式 （页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸大致为595×842点）
     * @param pageIndex指明页号
     **/
    public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
        if (pageIndex != 0) {
            return NO_SUCH_PAGE;
        }
        if (isFirst) {
            isFirst = false;
            return PAGE_EXISTS;
        }

        Graphics2D g = (Graphics2D) gra;

        g.setColor(Color.black);

        float[] dash1 = { 2.0f };
        // 设置打印线的属性。
        // 1.线宽 2、3、不知道，4、空白的宽度，5、虚线的宽度，6、偏移量
        g.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));

        float x = (float) pf.getImageableX();
        float y = (float) pf.getImageableY();

        final float leftOffset = x + getPoint(5);
        
        float heightOffset = y + getPoint(5);

        g.setFont(getFont(6));
        g.drawString("孝昕菜园", x + getPoint(16), heightOffset);

        heightOffset += getPoint(6);
        g.setFont(getFont(5));
        g.drawString("线下销售小票", x + getPoint(14), heightOffset);

        g.setFont(getFont(3));
        heightOffset += getPoint(7);
        g.drawString("订单编号:", leftOffset, heightOffset);
        
        heightOffset += getPoint(5);
        g.drawString(orderInfo.getOrderSn(), leftOffset, heightOffset);
        
        final float skuNumOffset = leftOffset;
        
        heightOffset += getPoint(5);

        final float skuNameOffset = x + getPoint(15);
        final float quantityOffset = skuNumOffset;
        final float priceOffset = x + getPoint(18);
        final float totleOffset = x + getPoint(35);

        g.drawString("货号", skuNumOffset, heightOffset);
        g.drawString("货号名称", skuNameOffset, heightOffset);
        
        heightOffset += getPoint(5);
        
        g.drawString("数量", quantityOffset, heightOffset);
        g.drawString("单价", priceOffset, heightOffset);
        g.drawString("金额", totleOffset, heightOffset);
        
        heightOffset += getPoint(5);
        
        g.drawString("(Kg)", quantityOffset, heightOffset);
        g.drawString("(元/Kg)", priceOffset, heightOffset);
        g.drawString("(元)", totleOffset, heightOffset);

        float nowHeightOffset = heightOffset + getPoint(5);
        for (GoodsInfo goodsInfo : orderInfo.getGoodsInfos()) {

            g.drawString(goodsInfo.getSkuNum(), skuNumOffset, nowHeightOffset);

            if (goodsInfo.getSkuName().length() > 10) {
                g.drawString(goodsInfo.getSkuName().substring(0, 11), skuNameOffset, nowHeightOffset);
            } else {
                g.drawString(goodsInfo.getSkuName(), skuNameOffset, nowHeightOffset);
            }
            
            nowHeightOffset += getPoint(4);
            
            g.drawString(goodsInfo.getQuantity(), quantityOffset, nowHeightOffset);
            
            BigDecimal price = new BigDecimal(goodsInfo.getPrice());
            
            g.drawString(price.setScale(2, RoundingMode.HALF_UP).toString(), priceOffset, nowHeightOffset);
            
            BigDecimal totlePrice = new BigDecimal(goodsInfo.getTotlePrice());
            
            g.drawString("￥" +  totlePrice.setScale(2, RoundingMode.HALF_UP), totleOffset, nowHeightOffset);
            
            nowHeightOffset += getPoint(4);
        }

        g.setFont(getFont(4));

        nowHeightOffset += getPoint(10);

        g.drawString("合计金额:￥" + orderInfo.getOrderAmount(), leftOffset, nowHeightOffset);

        nowHeightOffset += getPoint(6);

        g.drawString("门店:" + orderInfo.getStoreName(), leftOffset, nowHeightOffset);

        // g.setColor(Color.WHITE);

        nowHeightOffset += getPoint(15);

        g.drawString("------------", leftOffset, nowHeightOffset);

        return PAGE_EXISTS;
    }

    private static Font getFont(int fontSize) {
        return new Font(FONT_NAME, FONT_TYPE, getPoint(fontSize));
    }

}
